<?php
/**
 *    Parser for command line arguments. Extracts
 *    the a specific test to run and engages XML
 *    reporting when necessary.
 */
class QStone_CommandLineParser {
    private $xml = false;
    private $help = false;
    private $config = false;

    /**
     *    Parses raw command line arguments into object properties.
     *    @param string $arguments        Raw commend line arguments.
     */
    function __construct($arguments) {
        if (! is_array($arguments)) {
            return;
        }
        foreach ($arguments as $i => $argument) {
            if (preg_match('/^--?(date|d)=(.+)$/', $argument, $matches)) {
                $context = QStone_Context::getInstance();
                $context->date = $matches[2];
            } elseif (preg_match('/^--?(xml|x)$/', $argument)) {
                $this->xml = true;
            } elseif (preg_match('/^--?(help|h)$/', $argument)) {
                $this->help = true;
            } elseif (preg_match('/^--?(config|c)$/', $argument)) {
                $this->config = true;
            }

        }
    }
    
    /**
     *    the test case is configed by xml files
     *    @return boolean        True if XML desired.
     */
    function isConfig() {
        return $this->config;
    }

    /**
     *    Output should be XML or not.
     *    @return boolean        True if XML desired.
     */
    function isXml() {
        return $this->xml;
    }

    /**
     *    Output should be a help message. Disabled during XML mode.
     *    @return boolean        True if help message desired.
     */
    function help() {
        return $this->help && ! $this->xml;
    }

    /**
     *    Returns plain-text help message for command line runner.
     *    @return string         String help message
     */
    function getHelpText() {
        return <<<HELP
QStoneTest command line default reporter (autorun)
Usage: php <test_file> [args...]

    -x              Return test results in XML
    -h              Display this help message
    -d=yyyymmdd     Inject the date param to the test method


HELP;
    }
}
?>
